//
//  KKChatEmoticonView.m
//  ILiveDemo
//
//  Created by liaozhenming on 2017/4/5.
//  Copyright © 2017年 liaozhenming. All rights reserved.
//

#import "KKChatEmoticonView.h"

#import "KKLiveChatEmotionManager.h"

@interface KKChatEmoticonItemCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView* img_item;

@end

@implementation KKChatEmoticonItemCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        _img_item = [[UIImageView alloc] initWithFrame: CGRectMake(10, 10, CGRectGetWidth(frame) - 20, CGRectGetHeight(frame) - 20)];
        _img_item.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//        _img_item.center = CGPointMake(CGRectGetWidth(frame)/2, CGRectGetHeight(frame)/2);
        [self.contentView addSubview: _img_item];
    }
    return self;
}

@end


@interface KKChatEmoticonView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    UICollectionView *_collectionView;
}

@end

@implementation KKChatEmoticonView

+ (instancetype)kk_chatEmoticonViewWithItems:(NSArray*)items {
    CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    KKChatEmoticonView *functionView = [[KKChatEmoticonView alloc] initWithFrame: CGRectMake(0, 0, screenWidth, 216)];
    functionView.backgroundColor = [UIColor whiteColor];
    return functionView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [KKLiveChatEmotionManager manager];
        
        UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
        flowLayout.itemSize = CGSizeMake(50, 50);
        flowLayout.minimumLineSpacing = 5;
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        
        _collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake(0, 5, CGRectGetWidth(frame), CGRectGetHeight(frame) - 50) collectionViewLayout: flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass: [KKChatEmoticonItemCell class] forCellWithReuseIdentifier: @"KKChatEmoticonItemCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self addSubview: _collectionView];
        
        
        UIView *bottomBar = [[UIView alloc] initWithFrame: CGRectMake(0, CGRectGetHeight(frame) - 40, CGRectGetWidth(frame), 40)];
        bottomBar.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
        {
            UIButton *btn_send = [UIButton buttonWithType: UIButtonTypeCustom];
            btn_send.frame = CGRectMake(CGRectGetWidth(frame) - 64, 0, 64, CGRectGetHeight(bottomBar.bounds));
            btn_send.titleLabel.font = [UIFont systemFontOfSize: 14];
            [btn_send setTitle: @"发送" forState: UIControlStateNormal];
            [btn_send setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
            [btn_send setTitleColor: [UIColor lightGrayColor] forState: UIControlStateHighlighted];
            [btn_send setBackgroundColor: [UIColor colorWithRed:0 green:112/255.0 blue:1.0 alpha:1.0]];
            [btn_send addTarget: self action: @selector(action_send:) forControlEvents: UIControlEventTouchUpInside];
            [bottomBar addSubview: btn_send];
        }
//        {
//            UIButton *btn_delete = [UIButton buttonWithType: UIButtonTypeCustom];
//            btn_delete.frame = CGRectMake(CGRectGetWidth(frame) - 128, 0, 64, CGRectGetHeight(bottomBar.bounds));
//            btn_delete.titleLabel.font = [UIFont systemFontOfSize: 14];
//            [btn_delete setTitle: @"删除" forState: UIControlStateNormal];
//            [btn_delete setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
//            [btn_delete setTitleColor: [UIColor lightGrayColor] forState: UIControlStateHighlighted];
//            [btn_delete setBackgroundColor: [UIColor redColor]];
//            [btn_delete addTarget: self action: @selector(action_delete:) forControlEvents: UIControlEventTouchUpInside];
//            [bottomBar addSubview: btn_delete];
//        }
        
        [self addSubview: bottomBar];
    }
    return self;
}

- (void)action_send:(UIButton*)sender {
    if (self.didSendHandle) {
        self.didSendHandle();
    }
}

- (void)action_delete:(UIButton*)sender {
    if (self.didDeleteHandle) {
        self.didDeleteHandle();
    }
}

- (void)kk_reloadData {
    [_collectionView reloadData];
}

#pragma mark - UICollectionView methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[[KKLiveChatEmotionManager manager] arr_emoticonImageName] count];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KKChatEmoticonItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"KKChatEmoticonItemCell" forIndexPath: indexPath];
    
    cell.img_item.image = [[KKLiveChatEmotionManager manager] kk_chatEmoticonWithIndex: indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.didSelectedHandle) {
        
        UIImage *image = [[KKLiveChatEmotionManager manager] kk_chatEmoticonWithIndex: indexPath.row];
        self.didSelectedHandle(indexPath.row, image);
    }
}

@end


@interface KKCHatGifEmoticonView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    UICollectionView *_collectionView;
}

@end

@implementation KKCHatGifEmoticonView

+ (instancetype)kk_chatGifEmoticonViewWithItems:(NSArray*)items {
    CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    KKCHatGifEmoticonView *functionView = [[KKCHatGifEmoticonView alloc] initWithFrame: CGRectMake(0, 0, screenWidth, 216)];
    functionView.backgroundColor = [UIColor whiteColor];
    return functionView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [KKLiveChatEmotionManager manager];
        
        UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
        flowLayout.itemSize = CGSizeMake(70, 70);
//        flowLayout.minimumLineSpacing = 5;
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        
        _collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake(0, 5, CGRectGetWidth(frame), CGRectGetHeight(frame) - 10) collectionViewLayout: flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass: [KKChatEmoticonItemCell class] forCellWithReuseIdentifier: @"KKChatEmoticonItemCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self addSubview: _collectionView];
    }
    return self;
}

- (void)kk_reloadData {
    [_collectionView reloadData];
}

#pragma mark - UICollectionView methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[[KKLiveChatEmotionManager manager] arr_gifImageName] count];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KKChatEmoticonItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"KKChatEmoticonItemCell" forIndexPath: indexPath];
    
    cell.img_item.image = [[KKLiveChatEmotionManager manager] kk_gifEmoticonWithIndex: indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.didSelectedHandle) {
        NSString *imagePath = [[KKLiveChatEmotionManager manager] kk_gifImagePathWithIndex: indexPath.row];
        self.didSelectedHandle(indexPath.row, imagePath);
    }
}

@end
